$(function(){
    /***********************************/
    $('#Login-btn').on('click',function () {
        Login_fn();
    });
    $('#zhuCe-btn').on('click',function () {
        window.location.href = './zhuCe.html';
    });
    // 登录请求 and 判断
    function Login_fn() {
        /***********************************/
        let USER_NAME =  $('#userName').val().trim(),
            PASS_WORD = $('#userPw').val().trim();

        if( (USER_NAME !== '') && (PASS_WORD !== '') ){
            $.ajax({
                url: '/user/login.do',
                type: 'post',
                data: {
                    username: USER_NAME,
                    userpwd: PASS_WORD
                },
                xhrFields : {
                    withCredentials: true
                },
                crossDomain: true,
                success: function (data) {
                    console.log(data);
                    if(data.status == 0 ){
                        setCookie( data.data );
                        // 跳转
                        switch ( data.data.groupno ){
                            case 1 :
                                pointInfo( 1);
                                break;
                            case 2 :
                            case 3 :
                                pointInfo(2);
                                break;
                            case 4 :
                                pointInfo(4);
                                break;
                            default :
                                console.log('请求出错');
                        }

                    }else{
                        spop_er( data.msg, function () {
                            $('#userPw').val('');
                            // $('#userName').val('');
                        } );
                        return false;
                    }
                }
            });
        }
        // 判断输入是否 空
        else{
            if( !USER_NAME && !PASS_WORD ){
                spop_er( '输入不能为空' );
                return false;
            }else if(!USER_NAME){
                spop_er( '请输入用户名/学号',function (){}, function () {
                    $('#userPw').val('');
                } );
                return false;
            }else if(!PASS_WORD){
                spop_er( '请输入密码', function () {
                    $('#userPw').val('');
                } );
                return false;
            }
        }

    }

    function pointInfo( status ) {
        let str = '登录成功';
        // 页面跳转提示框
        switch (status ){
            case 1 :
                spop_ok( str,function () {},function () {
                    location.replace('./shenBao-index.html');
                } );
                break;
            case 2 :
                spop_ok( str,function(){},function () {
                    location.replace('./Admin/admin-index.html');
                } );break;
            case 4 :
                spop_ok( str,function(){},function () {
                    alert('专家页面');
                    // window.location.href='./Admin/admin-index.html';
                } );break;
        }
    }
});